import { Component } from '@angular/core';
import { NavController, NavParams, AlertController } from 'ionic-angular';
import { HomePage } from '../home/home';
import { HttpService } from '../../providers/http.service';
import { constant } from '../../providers/config';
import { CommonFunc } from '../../providers/commonFunc';
import { Storage } from '@ionic/storage';
import { TractionPage } from '../maince-type/traction/traction';
import { HydpressPage } from '../maince-type/hydpress/hydpress';
import { DebrisPage } from '../maince-type/debris/debris';
import { EscalatorPage } from '../maince-type/escalator/escalator';
/**
 * 维保历史
 */
@Component({
  templateUrl: 'maince-history.html'
})
export class MainceHistory {
  private list: any;
  private elevatorType;
  private elevatorNum;
  private elevatorId;
  private maintenceTypes = constant.maintenceTypes;
  private pageSize: number = 30;
  private currentPage: number = 1;
  private currentUserId: any;
  private elevatorIdInfo: any = {};
  constructor(
    public navCtrl: NavController,
    private alertCtrl: AlertController,
    private httpService: HttpService,
    private navParams: NavParams,
    private commonFunc: CommonFunc,
    private storage: Storage,
  ) {

    this.elevatorNum = navParams.get('elevatorNum');
    this.elevatorType = navParams.get('elevatorType');
    this.elevatorId = navParams.get('elevatorId');
   
  }
  ionViewWillEnter(){
    this.httpService.queryElevatorInfo({ id: this.elevatorId }).subscribe(data => {
      this.elevatorIdInfo = data.data;
      console.log(this.elevatorIdInfo)
  })
    //维保记录列表
    let ob: any;
    if (this.elevatorType == '1' || this.elevatorType == '5') {
      ob = this.httpService.queryTraction(
        { 'elevatorNum': this.elevatorNum, 'pageSize': this.pageSize, 'currentPage': this.currentPage }
      );
    }
    if (this.elevatorType == '2') {
      ob = this.httpService.queryHydpress(
        { 'elevatorNum': this.elevatorNum, 'pageSize': this.pageSize, 'currentPage': this.currentPage }
      );
    }
    if (this.elevatorType == '3') {
      ob = this.httpService.queryDebris(
        { 'elevatorNum': this.elevatorNum, 'pageSize': this.pageSize, 'currentPage': this.currentPage }
      );
    }
    if (this.elevatorType == '4' || this.elevatorType == '6') {
      ob = this.httpService.queryEscalator(
        { 'elevatorNum': this.elevatorNum, 'pageSize': this.pageSize, 'currentPage': this.currentPage }
      );
    }
    ob.subscribe((data) => {
      this.list = data.data.list;
    });
  }
  //刷新
  doRefresh(refresher) {
    setTimeout(() => {
      this.currentPage++;
      let params: any = {};
      params.pageSize = this.pageSize + '';
      params.currentPage = this.currentPage + '';
      params.elevatorNum = this.elevatorNum;

      let ob: any;
      if (this.elevatorType == '1' || this.elevatorType == '5') {
        ob = this.httpService.queryTraction(params);
      }
      if (this.elevatorType == '2') {
        ob = this.httpService.queryHydpress(params);
      }
      if (this.elevatorType == '3') {
        ob = this.httpService.queryDebris(params);
      }
      if (this.elevatorType == '4' || this.elevatorType == '6') {
        ob = this.httpService.queryEscalator(params);
      }

      ob.subscribe(data => {
        if (data.status != 200) {
          let toast = this.commonFunc.showInsideErrorToast(data.message);
          toast.present();
          return;
        }
        if (data.data.list.length <= 0) {
          refresher.enabled = false;
          setTimeout(() => {
            refresher.enabled = true;
          }, 3000);
        } else {
          data.data.list.forEach(e => {
            this.list.unshift(e);
          });
        }
      });

      refresher.complete();
    }, 2000);

  }

  todetail(eleInfo) {
    let elevatorType = this.elevatorType;
    let id = eleInfo.id;
    if (elevatorType == '1' || elevatorType == '5') {
      this.httpService
        .queryTractionDetails(id)
        .subscribe(dd => {
          if (dd.data.traction.tractionHalfMon.type * 1 >= 1) {
            this.storage.set(this.elevatorNum + '-his-' +
              'traction-half',
              this.commonFunc.convertBean(dd.data.traction.tractionHalfMon)
            );
            // this.summary = dd.data.traction.tractionHalfMon.summary;
            // this.changeIfno = dd.data.traction.tractionHalfMon.changeIfno;
            // this.safeUserSignId2 =
            //   dd.data.traction.tractionHalfMon.safeUserSignId2;
            // this.suggest = dd.data.traction.tractionHalfMon.usedPartyOpt;
          }
          if (dd.data.traction.tractionHalfMon.type * 1 > 1) { 
            this.storage.set(this.elevatorNum + '-his-' +
              'traction-quarter',
              this.commonFunc.convertBean(dd.data.traction.tractionQuarter)
            );
          }
          if (dd.data.traction.tractionHalfMon.type * 1 > 2) {
            this.storage.set(this.elevatorNum + '-his-' +
              'traction-half-year',
              this.commonFunc.convertBean(dd.data.traction.tractionHalfYear)
            );
          }
          if (dd.data.traction.tractionHalfMon.type * 1 > 3) {
            this.storage.set(this.elevatorNum + '-his-' +
              'traction-year',
              this.commonFunc.convertBean(dd.data.traction.tractionYear)
            );
          }

          let maitType = dd.data.traction.tractionHalfMon.type;
          this.navCtrl.push(TractionPage, {
            elevatorNum: this.elevatorNum,
            ElevaType: elevatorType,
            type: maitType,
            elevatorInfo : this.elevatorIdInfo,
            isLook: true,
          });
        });
    }

    if (elevatorType == '2') {
      this.httpService
        .queryHydpressDetails(id)
        .subscribe(dd => {
          if (dd.data.hydpress.hydpressHalfMon.type * 1 >= 1) {
            this.storage.set(this.elevatorNum + '-his-' +
              'hydpress-half',
              this.commonFunc.convertBean(dd.data.hydpress.hydpressHalfMon)
            );
            // this.summary = dd.data.hydpress.hydpressHalfMon.summary;
            // this.changeIfno = dd.data.hydpress.hydpressHalfMon.changeIfno;
            // this.safeUserSignId2 =
            //   dd.data.hydpress.hydpressHalfMon.safeUserSignId2;
            // this.suggest = dd.data.hydpress.hydpressHalfMon.usedPartyOpt;
          }
          if (dd.data.hydpress.hydpressHalfMon.type * 1 > 1) {
            this.storage.set(this.elevatorNum + '-his-' +
              'hydpress-quarter',
              this.commonFunc.convertBean(dd.data.hydpress.hydpressQuarter)
            );
          }
          if (dd.data.hydpress.hydpressHalfMon.type * 1 > 2) {
            this.storage.set(this.elevatorNum + '-his-' +
              'hydpress-half-year',
              this.commonFunc.convertBean(dd.data.hydpress.hydpressHalfYear)
            );
          }
          if (dd.data.hydpress.hydpressHalfMon.type * 1 > 3) {
            this.storage.set(this.elevatorNum + '-his-' +
              'hydpress-year',
              this.commonFunc.convertBean(dd.data.hydpress.hydpressYear)
            );
          }

          let maitType = dd.data.hydpress.hydpressHalfMon.type;
          this.navCtrl.push(HydpressPage, {
            elevatorNum: this.elevatorNum,
            ElevaType: elevatorType,
            type: maitType,
             elevatorInfo : this.elevatorIdInfo,
            isLook: true
          });
        });
    }
    if (elevatorType == '3') {
      this.httpService
        .queryDebrisDetails(id)
        .subscribe(dd => {
          if (dd.data.debris.debrisHalfMon.type * 1 >= 1) {
            this.storage.set(this.elevatorNum + '-his-' +
              'debris-half',
              this.commonFunc.convertBean(dd.data.debris.debrisHalfMon)
            );
            // this.summary = dd.data.debris.debrisHalfMon.summary;
            // this.changeIfno = dd.data.debris.debrisHalfMon.changeIfno;
            // this.safeUserSignId2 =
            //   dd.data.debris.debrisHalfMon.safeUserSignId2;
            // this.suggest = dd.data.debris.debrisHalfMon.usedPartyOpt;
          }
          if (dd.data.debris.debrisHalfMon.type * 1 > 1) {
            this.storage.set(this.elevatorNum + '-his-' +
              'debris-quarter',
              this.commonFunc.convertBean(dd.data.debris.debrisQuarter)
            );
          }
          if (dd.data.debris.debrisHalfMon.type * 1 > 2) {
            this.storage.set(this.elevatorNum + '-his-' +
              'debris-half-year',
              this.commonFunc.convertBean(dd.data.debris.debrisHalfYear)
            );
          }
          if (dd.data.debris.debrisHalfMon.type * 1 > 3) {
            this.storage.set(this.elevatorNum + '-his-' +
              'debris-year',
              this.commonFunc.convertBean(dd.data.debris.debrisYear)
            );
          }

          let maitType = dd.data.debris.debrisHalfMon.type;
          this.navCtrl.push(DebrisPage, {
            elevatorNum: this.elevatorNum,
            ElevaType: elevatorType,
            type: maitType,
              elevatorInfo : this.elevatorIdInfo,
            isLook: true
          });
        });
    }

    if (elevatorType == '4' || elevatorType == '6') {
      this.httpService
        .queryEscalatorDetails(id)
        .subscribe(dd => {
          if (dd.data.escalator.escalatorHalfMon.type * 1 >= 1) {
            this.storage.set(this.elevatorNum + '-his-' +
              'escalator-half',
              this.commonFunc.convertBean(dd.data.escalator.escalatorHalfMon)
            );
            // this.summary = dd.data.escalator.escalatorHalfMon.summary;
            // this.changeIfno = dd.data.escalator.escalatorHalfMon.changeIfno;
            // this.safeUserSignId2 =
            //   dd.data.escalator.escalatorHalfMon.safeUserSignId2;
            // this.suggest = dd.data.escalator.escalatorHalfMon.usedPartyOpt;
          }
          if (dd.data.escalator.escalatorHalfMon.type * 1 > 1) {
            this.storage.set(this.elevatorNum + '-his-' +
              'escalator-quarter',
              this.commonFunc.convertBean(dd.data.escalator.escalatorQuarter)
            );
          }
          if (dd.data.escalator.escalatorHalfMon.type * 1 > 2) {
            this.storage.set(this.elevatorNum + '-his-' +
              'escalator-half-year',
              this.commonFunc.convertBean(dd.data.escalator.escalatorHalfYear)
            );
          }
          if (dd.data.escalator.escalatorHalfMon.type * 1 > 3) {
            this.storage.set(this.elevatorNum + '-his-' +
              'escalator-year',
              this.commonFunc.convertBean(dd.data.escalator.escalatorYear)
            );
          }

          let maitType = dd.data.escalator.escalatorHalfMon.type;
          this.navCtrl.push(EscalatorPage, {
            elevatorNum: this.elevatorNum,
            ElevaType: elevatorType,
            type: maitType,
            elevatorInfo : this.elevatorIdInfo,
            isLook: true
          });
        });
    }
  }

}
